﻿using JST.TPLMS.Contract.IBaseFiles;
using JST.TPLMS.DataBase;
using JST.TPLMS.Entitys.Entities;
using JST.TPLMS.Entitys.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.JSInterop.Implementation;
using NPOI.OpenXmlFormats.Dml;

namespace JST.TPLMS.Web.Controllers
{
    /// <summary>
    /// 货物
    /// </summary>
    public class CargoController : Controller
    {
        private readonly IUnitOfWork _unitOfWork;

        public CargoController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }


        public IActionResult Index()
        {
            var cargos = _unitOfWork.CargoRepo.GetAll(includeProperties: "Supplier");
            return View(cargos);
        }

        #region 创建货物

        public IActionResult Create()
        {
            CargoVM cargoVM = new CargoVM()
            {
                SupplierList = _unitOfWork.SupplierRepo.GetAll().Select(u => new SelectListItem
                {
                    Text = u.Name,
                    Value = u.Id.ToString()
                })
            };
            return View(cargoVM);
        }

        [HttpPost]
        public IActionResult Create(CargoVM obj)
        {
            if (ModelState.IsValid)
            {
                _unitOfWork.CargoRepo.Add(obj.Cargo);
                _unitOfWork.Save();
                return RedirectToAction(nameof(Index));
            }
            obj.SupplierList = _unitOfWork.SupplierRepo.GetAll().Select(u => new SelectListItem
            {
                Text = u.Name,
                Value = u.Id.ToString()
            });
            return View(obj);
        }

        #endregion

        #region 更新货物

        public IActionResult Update(int cargoId)
        {
            CargoVM cargoVM = new()
            {
                SupplierList = _unitOfWork.SupplierRepo.GetAll().Select(u => new SelectListItem
                {
                    Text = u.Name,
                    Value = u.Id.ToString()
                }),
                Cargo = _unitOfWork.CargoRepo.Get(u => u.Id == cargoId)
            };

            if (cargoVM is null)
            {
                return RedirectToAction("Error", "Home");
            }
            return View(cargoVM);
        }


        [HttpPost]
        public IActionResult Update(CargoVM cargoVM)
        {
            if (ModelState.IsValid)
            {
                _unitOfWork.CargoRepo.Update(cargoVM.Cargo);
                _unitOfWork.Save();
                return RedirectToAction(nameof(Index));
            }
            cargoVM.SupplierList = _unitOfWork.SupplierRepo.GetAll().Select(u => new SelectListItem
            {
                Text = u.Name,
                Value = u.Id.ToString()
            });
            return View(cargoVM);
        }

        #endregion

        #region 删除货物

        public IActionResult Delete(int cargoId)
        {
            CargoVM cargoVM = new()
            {
                SupplierList = _unitOfWork.SupplierRepo.GetAll().Select(u => new SelectListItem
                {
                    Text = u.Name,
                    Value = u.Id.ToString()
                }),
                Cargo = _unitOfWork.CargoRepo.Get(u => u.Id == cargoId)
            };

            if (cargoVM is null)
            {
                return RedirectToAction("Error", "Home");
            }
            return View(cargoVM);
        }


        [HttpPost]
        public IActionResult Delete(CargoVM cargoVM)
        {
            Cargo? cargo = _unitOfWork.CargoRepo.Get(u => u.Id == cargoVM.Cargo.Id);
            if (cargo != null)
            {
                _unitOfWork.CargoRepo.Remove(cargo);
                _unitOfWork.Save();
                return RedirectToAction(nameof(Index));
            }
            return View(cargoVM);
        }

        #endregion
    }
}
